package util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {

    private static SqlSessionFactory sessionFactory;

    static {
        try {
            sessionFactory=new SqlSessionFactoryBuilder()
                .build(MyBatisUtil.class
                                  .getClassLoader()
                                  .getResourceAsStream("mybatis-config.xml"));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError("MyBatisUtil初始化失败");
        }
    }


    public static SqlSession getSession(){
        return sessionFactory.openSession();
    }


    public static void closeSession(SqlSession session){
        if(session!=null){
            session.close();
        }
    }
}
